package com.training.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.training.entities.Employee;

@Repository("employeeDAO")
public class EmployeeDAO {

	@PersistenceContext(unitName = "jsfSpring")
	EntityManager entityManager;

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	public Employee validateEmployee(String userName, String password) {

		Employee employee = (Employee) entityManager
				.createQuery(
						"Select e from Employee e where "
								+ "userName = :userName and password = :password")
				.setParameter("userName", userName)
				.setParameter("password", password).getSingleResult();

		return employee;

	}

	public void saveNewEmployee(Employee employee) {

		   entityManager.persist(employee);

	}

}
